package pageTest.login;

import constants.HostConstant;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

public class LoginPage extends WebDriverUtils {
    // 声明为静态变量，则该属性会先被初始化
    private static final String LOGIN_PAGE_URL = "http://" + HostConstant.HOST + ":5173/user/login";

    public LoginPage() {
        super(INIT_URL);
    }


    @BeforeMethod
    public void beforeEachTest() {
        driver.get(LOGIN_PAGE_URL);
    }

    @Test
    public void loginPageRight() {
        String topNavbarText = driver.findElement(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis")).getText();
        assert topNavbarText.equals("登录");
//        String loginButtonText = driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round > div")).getText();
//        assert loginButtonText.equals("登录");
    }


    @Test
    public void loginFail() throws InterruptedException {
        Actions actions = new Actions(driver);
        // 只能输入数字、字母、下划线、连字符
        // 账号名长度不小于4，密码长度不小于8
        WebElement accountElem = driver.findElement(By.cssSelector("#van-field-1-input"));
        WebElement passwordElem = driver.findElement(By.cssSelector("#van-field-2-input"));
        WebElement loginButton = driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round"));

        // 1. 账号不正确
        // 1) 账号为空
        passwordElem.sendKeys("12345678");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);
        // 2) 账号包含特殊字符
        accountElem.sendKeys("哈哈");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);
        // 3) 账号长度小于4
        inputClearAndSend(accountElem, "abc");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);
        // 4) 账号不存在
        inputClearAndSend(accountElem, "abcdefg");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);

        // 2. 密码不正确
        inputClearAndSend(accountElem, "cccc");
        // 1) 密码为空
        inputClearAndSend(passwordElem, " ");
        actions.moveToElement(loginButton).click().perform();
//        Thread.sleep(2000);
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);
        // 2) 密码包含特殊字符
        inputClearAndSend(passwordElem, "@@");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);
        // 3) 密码长度小于 8
        inputClearAndSend(passwordElem, "1234567");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
//        Thread.sleep(2000);
        // 4) 密码与账号不匹配
        inputClearAndSend(passwordElem, "123456789asd");
        actions.moveToElement(loginButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
    }


    @Test
    public void loginSuccess() {
        WebElement accountElem = driver.findElement(By.cssSelector("#van-field-1-input"));
        WebElement passwordElem = driver.findElement(By.cssSelector("#van-field-2-input"));
        WebElement loginButton = driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round"));
        inputClearAndSend(accountElem, "cccc");
        inputClearAndSend(passwordElem, "12345678");
        Actions actions = new Actions(driver);
        // 使用动作链点击元素
        actions.moveToElement(loginButton).click().perform();

        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 伙伴匹配"));
    }



    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}
